﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Apollo.Web;
using Apollo.Core;
using Apollo;

namespace Apollo.Website
{
    public partial class Controls_Edit_FormItemFilter : System.Web.UI.UserControl, IFormItem
    {
        public FormItem Item { get; set; }

        public object TargetObject { get; set; }

        protected string Columns
        {
            get
            {
                string colModel = "";

                Entity entity = null;

                if (this.TargetObject is Entity)
                    entity = this.TargetObject as Entity;
                else if (this.TargetObject is EntityView)
                    entity = (this.TargetObject as EntityView).Entity;

                if (entity == null)
                    return "";

                foreach (EntityProperty property in Apollo.Application.Meta<EntityProperty>().Where<EntityProperty>(ep => ep.Entity.Id == entity.Id))
                {
                    if (colModel != "")
                        colModel += ",";

                    colModel += "{ name: '" + property.PropertyName + "', fieldType: '" + (property.Type == EntityPropertyType.Relation ? "object" : "property") + "', displayName: '" + property.PropertyName + "'}";
                }

                return colModel;
            }
        }

        protected string Filter
        {
            get
            {
                if (this.TargetObject is Entity)
                {
                    if (!string.IsNullOrEmpty((this.TargetObject as Entity).Filter))
                        return (this.TargetObject as Entity).Filter;
                }
                else if (this.TargetObject is EntityView)
                {
                    if (!string.IsNullOrEmpty((this.TargetObject as EntityView).Filter))
                        return (this.TargetObject as EntityView).Filter;
                }

                return "null";
            }
        }

        protected int EntityID
        {
            get
            {
                if (this.TargetObject is Entity)
                    return (this.TargetObject as Entity).Id;
                else if (this.TargetObject is EntityView)
                    return (this.TargetObject as EntityView).Entity.Id;

                return -1;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                string filter = "";

                if (this.TargetObject is Entity)
                    filter = (this.TargetObject as Entity).Filter;
                else if (this.TargetObject is EntityView)
                    filter = (this.TargetObject as EntityView).Filter;

                this.HiddenFieldFilter.Value = filter;
                this.LabelPropertyName.Text = this.Item.Projection.DisplayName;
            }
        }

        public ActionMessage Save(object obj)
        {
            if (HiddenFieldFilter.Value != "")
            {
                string filterString = (
                    HiddenFieldFilter.Value == @"{""groupOp"":""AND"",""rules"":[],""groups"":[]}" ||
                    HiddenFieldFilter.Value == @"{""groupOp"":""OR"",""rules"":[],""groups"":[]}") ?
                    null : HiddenFieldFilter.Value;

                if (this.TargetObject is Entity)
                    (this.TargetObject as Entity).Filter = filterString;
                else if (this.TargetObject is EntityView)
                    (this.TargetObject as EntityView).Filter = filterString;
            }
            else
            {
                if (this.TargetObject is Entity && !string.IsNullOrEmpty((this.TargetObject as Entity).Filter))
                    (this.TargetObject as Entity).Filter = null;
                else if (this.TargetObject is EntityView && !string.IsNullOrEmpty((this.TargetObject as EntityView).Filter))
                    (this.TargetObject as EntityView).Filter = null;
            }

            return null;
        }
    }
}